package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.Technics;
import com.sdry.model.jyy.Shedu;
import com.sdry.model.jyy.SheduExample;
import com.sdry.service.hpf.TechnicsService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: TechnicsController 
 * @Description:工艺信息Controller
 * @Author hpf
 * @Date 2018年9月21日 上午11:23:57 
 * @version 1.0
 */
@RequestMapping(value="/technicsMana")
@Controller
public class TechnicsController {

	@Resource
	private TechnicsService technicsService;
	
	/**
	 * 分页查询
	 * @param page 分页条件
	 * @param technics 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageTechnics")
	public void listPageTechnics(Page page,Technics technics,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (null != technics.getTname() && !"".equals(technics.getTname())) {
			map.put("tname", technics.getTname());
		} else {
			map.put("tname", "");
			technics.setTname("");
		}
		int count = technicsService.countTechnics(technics);
		List<Technics> technicsList = technicsService.listPageTechnics(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(technicsList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	
	
	/**
	 * 保存工艺信息
	 * @param technics
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveTechnics")
	public void saveTechnics(Technics technics,HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (technics.getTid() == null || "".equals(technics.getTid())) {
			isSuccess = technicsService.insertTechnics(technics);
		} else {
			isSuccess = technicsService.updateTechnics(technics);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除工艺信息
	 * @param str 工艺信息ID组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteTechnics")
	public void deleteTechnics(@RequestParam("str") String str,HttpServletResponse response) throws Exception{
		int isSuccess = technicsService.deleteTechnics(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 校验是否删除工艺信息
	 * @param str 工艺信息ID组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteTechnicsOfVerify")
	public void deleteTechnicsOfVerify(@RequestParam("str") String str,HttpServletResponse response) throws Exception{
		int isSuccess = technicsService.deleteTechnicsOfVerify(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 根据ID查询工艺信息
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/getTechnicsById")
	public void getTechnicsById(Technics technics,HttpServletResponse response) throws Exception{
		if(null != technics.getTid() && !"".equals(technics.getTid())){
			Technics te = technicsService.getTechnicsById(technics.getTid());
			ResponseUtil.write(response, te.getRemark01());
		}
	}
	
	/**
	 * 根据名称查询工艺信息
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/getTechnicsByName")
	public void getTechnicsByName(Technics technics,HttpServletResponse response) throws Exception{
		if(null != technics.getTname() && !"".equals(technics.getTname())){
			List<Technics> list = technicsService.getTechnicsByName(technics.getTname());
			//将对象转化为json
	        JSONArray json = JSONArray.fromObject(list);
	        ResponseUtil.write(response, json);
		}
	}
	
	/**
	 * 查询所有
	 * @Title:listAll   
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/listAll")
	public void listAll(HttpServletResponse response) throws Exception {
		List<Technics> list = technicsService.listAllTechnics();
		//将对象转化为json
        JSONArray json = JSONArray.fromObject(list);
        ResponseUtil.write(response, json);
	}
	
}
